/**
 * 
 */
package ca.uwindsor.cs.deepweb.estimation;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.Hits;

/**
 * @author Liang Jie
 * 
 */
public class DataCollectorWithLimitationTop extends DataCollector {

	protected int limit;

	public DataCollectorWithLimitationTop(String fieldname, int limitation) {
		super(fieldname);
		limit = limitation;
	}

	public Set<String> add(Hits hits) {
		int_markedItemsinNewHits = 0;
		
		int lim = 0;
		if (hits.length() <= limit) {
			lim = hits.length();
			returnedItems = hits.length();
		} else {
			lim = limit;
			returnedItems = limit;
		}
		HashSet<String> newhitsids = new HashSet<String>(returnedItems);
		// get every Document in the Hits and add it to set_QueriedItems
		for (int offset = 0; offset < returnedItems; offset++) {
			try {
				String id = hits.doc(offset).get(string_FieldName);
				if (set_UniqueItems.contains(id)) {
					int_markedItemsinNewHits++;
				}
				set_UniqueItems.add(id);
				newhitsids.add(id);
				if (isAnalyzeDocumentFrequency) {
					dfanalyzer.addID(id);
				}
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// calculate the total items that queried
		long_TotalItems += lim;
		// compare with last hits
		set_previousHitsID.retainAll(newhitsids);
		int_DuplicatedItemsCompareToLastQuery = set_previousHitsID.size();
		// warning
		set_previousHitsID = (HashSet<String>) newhitsids.clone();
		return newhitsids;
	}

}
